<?php
namespace app\shop\controller\v1;
use think\Controller;
use think\Validate;
use think\Db;
use org\BaiduMap;
use think\Model;
use think\Loader;
use org\HMongodb;
use app\shop\GoodsLogic;
// use app\common\logic\GoodsPromFactory;
// use app\common\model\SpecGoodsPrice;
use think\Page;

class Goods extends Init
{
    protected function _initialize()
    {
        
        parent::_initialize();
        $this->GoodsModel = Loader::model('Goods');
        $this->GoodsLogicModel = Loader::model('GoodsLogic');
    }
    //获取商品列表
    public function goodsList(){
        $param = input('post.');
        $result = $this->validate($param, 'GoodsValidate.category');
            if (true !== $result) {
                return ['code'=>400,'message'=>$result,'data'=>[],'status'=>false];
            }
        $page = isset($param['page']) ? ($param['page'] == false ? 1 : $param['page'] ) : 1;
        $orll_page = isset($param['pageSize']) ? ($param['pageSize'] == false ? config('paginate.list_rows'): $param['pageSize'] ) : config('paginate.list_rows');
        if($param['prom_type'] == 3){
            $data = $this->GoodsModel->getPromotionGoods($param);
        }else{
            $data = $this->GoodsModel->getGoodsListData($param);
        }
        if($data){
            if($data){
                return ['data' => $data, 'code' => 200, 'message' => '列表显示成功', 'status' => true];
            }else{
                return ['data' => $data, 'code' => 400, 'message' => '没有更多的商品了', 'status' => false];
            }
        }else{
                return ['data' => [], 'code' => 400, 'message' => '暂时没有可售的商品', 'status' => false];
        }
    }
    
    public function goodsInfo(){
        $param = input('post.');
        $result = $this->validate($param, 'GoodsValidate.goods');
            if (true !== $result) {
                return ['code'=>400,'message'=>$result,'data'=>[],'status'=>false];
            }
            $goods_id = $param['goods_id'];
        $res = $this->GoodsModel->getGoodsDetail($param);
        return ['code'=>200,'message'=>'成功','data'=>$res,'status'=>true];
    }
    
    public function goodsSpecPrice(){
        $param = input('post.');
        $result = $this->validate($param, 'GoodsValidate.item');
            if (true !== $result) {
                return ['code'=>400,'message'=>$result,'data'=>[],'status'=>false];
            }
            $goods_id = $param['goods_id'];
        $res = $this->GoodsModel->getSpecGoodsPrice($param);
        if(count($res) > 0){
            return ['code'=>200,'message'=>'成功','data'=>$res,'status'=>true];
        }else{
            return ['code'=>400,'message'=>'没有该规格','data'=>$res,'status'=>false];
        }
    }
    
    public function goodsInfo1(){
        $goodsLogic = new GoodsLogic();
        $goods_id = I("post.id/d");
        $goodsModel = new \app\shop\model\Goods();
        $goods = $goodsModel::get($goods_id);
        if(empty($goods) || ($goods['is_on_sale'] == 0) || ($goods['is_virtual']==1 && $goods['virtual_indate'] <= time())){
            $this->error('此商品不存在或者已下架');
        }
        $goodsPromFactory = new GoodsPromFactory();
        if (!empty($goods['prom_id']) && $goodsPromFactory->checkPromType($goods['prom_type'])) {
            $goodsPromLogic = $goodsPromFactory->makeModule($goods, null);//这里会自动更新商品活动状态，所以商品需要重新查询
            $goods = $goodsPromLogic->getGoodsInfo();//上面更新商品信息后需要查询
        }
        if (cookie('user_id')) {
            $goodsLogic->add_visit_log(cookie('user_id'), $goods);
        }
        if($goods['brand_id']){
            $brnad = M('brand')->where("id", $goods['brand_id'])->find();
            $goods['brand_name'] = $brnad['name'];
        }
        $goods_images_list = M('GoodsImages')->where("goods_id", $goods_id)->select(); // 商品 图册
        $goods_attribute = M('GoodsAttribute')->getField('attr_id,attr_name'); // 查询属性
        $goods_attr_list = M('GoodsAttr')->where("goods_id", $goods_id)->select(); // 查询商品属性表
        $filter_spec = $goodsLogic->get_spec($goods_id);
        $spec_goods_price  = M('spec_goods_price')->where("goods_id", $goods_id)->getField("key,price,store_count,item_id"); // 规格 对应 价格 库存表
        $commentStatistics = $goodsLogic->commentStatistics($goods_id);// 获取某个商品的评论统计
        $this->assign('spec_goods_price', json_encode($spec_goods_price,true)); // 规格 对应 价格 库存表
        $goods['sale_num'] = M('order_goods')->where(['goods_id'=>$goods_id,'is_send'=>1])->count();
        //当前用户收藏
        $user_id = cookie('user_id');
        $collect = M('goods_collect')->where(array("goods_id"=>$goods_id ,"user_id"=>$user_id))->count();
        $goods_collect_count = M('goods_collect')->where(array("goods_id"=>$goods_id))->count(); //商品收藏数
        $this->assign('collect',$collect);
        $this->assign('commentStatistics',$commentStatistics);//评论概览
        $this->assign('goods_attribute',$goods_attribute);//属性值     
        $this->assign('goods_attr_list',$goods_attr_list);//属性列表
        $this->assign('filter_spec',$filter_spec);//规格参数
        $this->assign('goods_images_list',$goods_images_list);//商品缩略图
        $this->assign('goods',$goods->toArray());
        $point_rate = tpCache('shopping.point_rate');
        $this->assign('goods_collect_count',$goods_collect_count); //商品收藏人数
        $this->assign('point_rate', $point_rate);
        return $this->fetch();
    }
}